package com.youxin.cpcenter.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.enums.OrderPayTypeEnums;
import com.youxin.busfoundation.enums.ProductsCouponEnums;
import com.youxin.cpcenter.bean.RequestOrderInfoBean;
import com.youxin.cpcenter.bean.RequestTradeQueryBean;
import com.youxin.cpcenter.common.log.LogMethod;
import com.youxin.cpcenter.model.TbOrderInfo;
import com.youxin.cpcenter.service.TransactionService;
import com.youxin.cpcenter.service.WxPayService;
import com.youxin.foundation.utils.ObjectUtils;

/**
 * @ClassName: WeixinPayController 
 * @Description: 微信支付 
 * @author yingde.cao
 * @date 2017年11月27日 上午9:21:04 
 *
 */
@RestController
public class WxPayController {
	private static final Logger logger = LoggerFactory.getLogger(WxPayController.class);
	@Autowired
	private WxPayService wxPayService;
	@Autowired
	private TransactionService transactionService;
	
	/**
	 * @Title: qcPay2 
	 * @Description: 二维码支付(模式二)下单并生成二维码
	 * @param @param product
	 * @param @param map
	 * @param @return
	 * @return String
	 * @throws
	 */
	@LogMethod(id="ent.api.weixin.qcpay",eventContent={})
	@RequestMapping(value="/api/weixin/qcpay",method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseObject qcPay2(HttpServletRequest request, HttpServletResponse response,
			@RequestBody RequestOrderInfoBean requestBean) {
		try {
			ObjectUtils.requestNonEmpty(requestBean.getOrderType().toString(), "订单类型为空");
			//orderType类型为 充值金券 产品ID为必传
			if(OrderPayTypeEnums.CZJJ.getIndex() == requestBean.getOrderType()) {
				ObjectUtils.requestNonEmpty(requestBean.getProductId().toString(), "产品ID为空");
				ObjectUtils.requestNonEmpty(ProductsCouponEnums.getName(requestBean.getProductId()), "产品ID不正确");
			}
			ObjectUtils.requestNonEmpty(ProductsCouponEnums.getName(requestBean.getProductId()), "产品ID不正确");
			return wxPayService.weixinPay2(request,response,requestBean);
		} catch (Exception e) {
			return ResponseObject.systemError(e);
		}
    }

	/**
	 * @Title: weixin_notify 
	 * @Description: 支付后台回调
	 * @param @param request
	 * @param @param response
	 * @param @throws Exception
	 * @return void
	 * @throws
	 */
	@LogMethod(id="ent.api.weixin.notify",eventContent={})
	@RequestMapping(value = "/api/weixin/notify", method = RequestMethod.POST)
	public String weixin_notify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.warn("微信支付回调");
		String rs = "";
		try {
			rs = wxPayService.weixinPayNotify(request);
		} catch (Exception e) {
			logger.error("/api/weixin/notify异常", e);
			return rs;
		}
		return rs;
	}
	/**
	 * @Title: tradeQuery 
	 * @Description: 微信交易结果查询
	 * @param @param request
	 * @param @param response
	 * @param @param requestBean
	 * @param @return
	 * @return ResponseObject
	 * @throws
	 */
	@LogMethod(id="ent.api.weixin.trade.query",eventContent={})
	@RequestMapping(value="/api/weixin/trade/query",method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseObject tradeQuery(HttpServletRequest request, HttpServletResponse response,
			@RequestBody RequestTradeQueryBean requestBean) {
		logger.info("微信交易结果查询");
		try {
			ObjectUtils.requestNonEmpty(requestBean.getOrderSn(), "商户订单号为空");
			TbOrderInfo model = transactionService.queryWx(requestBean.getOrderSn());
			return transactionService.procesResponseStatus(model);
		} catch (Exception e) {
			logger.error("/api/weixin/tradeQuery异常", e);
			return ResponseObject.systemError(e);
		}
    }
	
}
